﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;

namespace System.Configuration
{
    public class AppSettingsWriter
    {
        private string _configFileName;
        private XmlDocument _configDocument = new XmlDocument();

        public AppSettingsWriter() 
        {
            Assembly asmy = Assembly.GetEntryAssembly();
            _configFileName = asmy.Location + ".config";
            Reset();
        }

        public void SetNewValue(string key, string value)
        {
            string xpath = string.Format("/configuration/appSettings/add[@key=\"{0}\"]", key);
            XmlNode node = _configDocument.SelectSingleNode(xpath);

            if (node == null)
            {
                XmlElement element = _configDocument.CreateElement("add");
                element.SetAttribute("key", key);
                element.SetAttribute("value", value);

                xpath = "/configuration/appSettings";
                XmlNode root = _configDocument.DocumentElement.SelectSingleNode(xpath);

                root.AppendChild((XmlNode)element);
            }
            else
            {
                node.Attributes.GetNamedItem("value").Value = value;
            }
        }

        public void Save()
        {
            _configDocument.Save(_configFileName);
        }

        public void Reset()
        {
            _configDocument.Load(_configFileName);
        }
    }
}
